import { createStore } from './createStore'

type AlertConfig = {
  title?: string
  description?: string
  confirmLabel?: string
  cancelLabel?: string
  onConfirm?: () => void
  onCancel?: () => void
}

type State = {
  alertOpen: boolean
  alertConfig: AlertConfig | null
}

type Actions = {
  updateAlertOpen: (is: State['alertOpen']) => void
  showAlert: (config: AlertConfig) => void
}
const useGlobalStore = createStore<State & Actions>(
  set => ({
    alertOpen: false,
    alertConfig: null,
    updateAlertOpen: is =>
      set(state => {
        state.alertOpen = is
        if (!is) state.alertConfig = null
      }),
    showAlert: config =>
      set(() => {
        return { alertOpen: true, alertConfig: config }
      })
  }),
  { name: 'global-store', excludeFromPersist: ['alertOpen'] }
)
const alert = (config: AlertConfig) => {
  useGlobalStore.getState().showAlert(config)
}

export { useGlobalStore, alert }
